package com.wc.acwing.思维.哞叫时间II;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/2/20 17:14
 * @description
 * https://www.acwing.com/problem/content/6137/
 */
public class Main {
    /**
     * 思路：
     * 记录每个数最后一次出现, 再记录倒数第二次出现,
     * 然后遍历, 观察前面有多少个不同的数
     * 当遍历到第二次出现的数, 计算前面有多少个不同的数,
     * 并且不等于该数, 这样每个数是会被记录一次, 并且是最优的
     */

    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1000010;
    static int[] a = new int[N];
    static int[] last = new int[N], sec = new int[N];
    static boolean[] st = new boolean[N];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
        // 记录最后一次出现
        for (int i = 1; i <= n; i++) last[a[i]] = i;
        // 记录倒数第二次出现
        for (int i = 1; i <= n; i++) {
            if (last[a[i]] != i) sec[a[i]] = i;
        }
        long res = 0, nrp = 0;

        for (int i = 1; i <= n; i++) {
            // 当遍历到倒数第二个的时候, 计算答案
            if (sec[a[i]] == i) {
                res += nrp;
                if (st[a[i]]) res--;
            }

            // 记录前面的不重复数字
            if (!st[a[i]]) {
                nrp++;
                st[a[i]] = true;
            }
        }
        out.println(res);
        out.flush();
    }
}


class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
